package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DangKou;
import com.ruoyi.common.core.domain.entity.DangKouUser;
import com.ruoyi.system.service.IDangkouService;
import com.ruoyi.system.service.IDangkouUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 食堂档口信息表 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-06
 */
@RestController
@RequestMapping("/system/dangkou")
public class DangkouController extends BaseController {

    @Resource
    private IDangkouService dangkouService;

    @Resource
    private IDangkouUserService dangkouUserService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DangKou user)
    {
        startPage();
        List<DangKou> list = dangkouService.selectDangKouList(user);
        for(DangKou dangkou:list){
            DangKouUser dangkouUser = dangkouUserService.getById(dangkou.getAdminId());
            if(dangkouUser!=null){
                dangkou.setAdminName(dangkouUser.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/listByAdmin")
    public AjaxResult listByAdmin(DangKou user)
    {
        AjaxResult ajax = AjaxResult.success();
        List<DangKou> list = dangkouService.selectByAdmin(user);
        ajax.put(AjaxResult.DATA_TAG, list);
        return ajax;
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll( )
    {
        AjaxResult ajax = AjaxResult.success();
        List<DangKou> list = dangkouService.selectAll();
        ajax.put(AjaxResult.DATA_TAG, list);
        return ajax;
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId))
        {
            DangKou user = dangkouService.getById(userId);
            ajax.put(AjaxResult.DATA_TAG, user);
        }
        return ajax;
    }


    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DangKou user)
    {
        user.setCreateBy(getUsername());
        user.setCreateTime(new Date());
        return toAjax(dangkouService.save(user));
    }


    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DangKou user)
    {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(dangkouService.updateById(user));
    }


    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(dangkouService.deleteDangkouByIds(userIds));
    }
}
